<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\home\model\WebChapter;
use app\home\model\WebUser;
use think\facade\Lang;
use think\facade\View;


class Index extends Base
{

    protected $webcate;
    protected $webchapter;
    protected $webuser;
    protected $webconfig;


    public function __construct(){

        parent::__construct();
        $this->webchapter = new WebChapter();
        $this->webuser = new WebUser();

        $this->setLang(!empty(input("lang")) ?  input("lang") : request()->cookie("xmcms_lang"));
        

    }

    public function index(){

        //echo request()->cookie("xmcms_lang");

        // 获取当前语言
        // echo Lang::getLangSet();

        // echo "<br/>";

//         //获取当前语言
// think\Facade\Lang::getLangSet();
// //获取默认语言
// think\Facade\Lang::defaultLangSet();
// //设置当前语言
// think\Facade\Lang::setLangSet("zh-cn");


        // cookie("xmcms_lang","zh-cn");
        // echo lang("Home");
        // echo lang("About us");
        // echo "<br />";

        $page = input("page") ? input("page")-1 : 0;
        $pagesize=10;

        $lang = Lang::getLangSet();
        
        $chapter = $this->webchapter->where(["status"=>1,"lang"=>$lang])->order("id","desc")->limit($page*$pagesize,$pagesize)->select()->toArray();
        $total = $this->webchapter->where(["status"=>1,"lang"=>$lang])->count();

        if(!empty($chapter)){
            for($i=0;$i<count($chapter);$i++){

                $chapter[$i]["content"]=strip($chapter[$i]["content"],0,100)."...";

                if(empty($chapter[$i]["cateid"])){
                    $chapter[$i]["category_name"]="未定义";
                }else{
                    $cate = $this->webcate->where(["id"=>$chapter[$i]["cateid"] ])->find();
                    if($cate){
                        $chapter[$i]["category_name"] = $cate->category_name ;
                    }else{
                        $chapter[$i]["category_name"] = "未定义";
                    }
                    
                }

                if(empty($chapter[$i]["cover"])){
                    if($cate){
                        $chapter[$i]["cover"] = setdefaultimage($cate->alias);
                    }
                }

                if(empty($chapter[$i]["postuid"])){
                    $chapter[$i]["nickname"]="未知";
                }else{
                    $user = $this->webuser->where(["id"=>$chapter[$i]["postuid"]])->find();
                    $chapter[$i]["nickname"]=$user->nickname;
                }

                $chapter[$i]["create_time"]=date("Y-m-d",strtotime($chapter[$i]["create_time"]));

            }
        }

        $rand_list = [];

        View::assign([
            "home"=>lang("Home"),
            "current"=>"0",
            "chapter"=>$chapter,
            "rand_list"=>$rand_list,
            "total"=>$total,
            "page"=>input("page"),
            "swiper"=>$this->getSwiper("index",3,$lang),
            "ad1"=>$this->getSwiper("ad1",1,$lang),
            "ad2"=>$this->getSwiper("ad2",1,$lang),
            "lang"=>$lang
        ]);

        return View::fetch();
    }

    public function category(){

        $id=input("id");
        $page = input("page") ? input("page")-1 : 0;
        $pagesize=10;

        $data = $this->webcate->where(["id"=>$id,"isshow"=>1])->find();

        if($data){
            $temp = $data->tempstatus ? $data->alias : "";
        }else{
            echo "输入有误";
            exit;
        }

        $lang = Lang::getLangSet();

        $cate_list = $this->webchapter->where(["cateid"=>$id,"lang"=>$lang])->order("id desc")->limit($page*$pagesize,$pagesize)->select()->toArray();
        $total = $this->webchapter->where(["cateid"=>$id,"lang"=>$lang])->count();
        $cate=$this->webcate->where(["id"=>$id])->find();

        if(!empty($cate_list)){
            for($i=0;$i<count($cate_list);$i++){
                $cate_list[$i]["category_name"]=$cate["category_name"];
                $user = $this->webuser->where(["id"=>$cate_list[$i]["postuid"]])->find();
                $cate_list[$i]["nickname"]=$user["nickname"];
                $cate_list[$i]["content"]=strip($cate_list[$i]["content"],0,100)."...";
                $cate_list[$i]["cover"] =$cate_list[$i]["cover"] ? $cate_list[$i]["cover"] : setdefaultimage($cate->alias);
            }
        }

        $rand_list =[];

        return View::fetch($temp,[

            "home"=>lang("Home"),

            "seo_title"=>$data["seo_title"],
            "seo_keywords"=>$data["keywords"],
            "seo_description"=>$data["descriptions"],
            "current"=>$id,
            "chapter"=>$cate_list,
            "cate"=>$cate,
            "rand_list"=>$rand_list,
            "total"=>$total,
            "page"=>input("page"),
            "lang"=>$lang
        ]);

    }


    public function artical(){

        $id=input("id");

        $lang = Lang::getLangSet();

        $this->webchapter->where(["id"=>$id])->inc("readnum",1)->update();

        $artical = $this->webchapter->where(["id"=>$id])->find();

        if(!empty($artical)){

            $artical->content = htmlspecialchars_decode($artical->content);
            
            $user = $this->webuser->where(["id"=>$artical->postuid])->find();

            $artical->nickname = $user->nickname;

            $list = $this->webchapter->where(["cateid"=>$artical->cateid,"lang"=>$lang])->order("id desc")->limit(12)->select()->toArray();
            
            for($i=0;$i<count($list);$i++){

                $cate = $this->webcate->where(["id"=>$list[$i]["cateid"]])->find();


                $list[$i]["cover"] = $list[$i]["cover"] ? $list[$i]["cover"]  : setdefaultimage($cate->alias);
            
                $user = $this->webuser->where(["id"=>$list[$i]["postuid"]])->find();

                $list[$i]["nickname"]=$user->nickname;

                $list[$i]["create_time"]=date("Y-m-d",strtotime($list[$i]["create_time"]));

            }

        }else{

            echo "输入有误";
            exit;
        }

        $rand_list = [];

        View::assign([

            "home"=>lang("Home"),
            
            "seo_title"=>$artical["title"],
            "seo_keywords"=>$artical["keywords"],
            "seo_description"=>$artical["descriptions"],

            "current"=>$artical->cateid,
            "artical"=>$artical,
            "list"=>$list,
            "rand_list"=>$rand_list,
            "lang"=>$lang
        ]);

        return View::fetch();
    }



    public function page(){

        View::assign([
            "current"=>0,
        ]);
        
        return View::fetch();
    }


    function about(){

        View::assign([
            "current"=>input("id")
        ]);

        return View::fetch();

    }


}
